<?php

class Farbtastic extends CInputWidget
{
  protected $pickerId;

  public function init()
  {
    list($this->htmlOptions['name'], $this->htmlOptions['id']) = $this->resolveNameID();

    $vendorDir = Yii::app()->getAssetManager()->publish($this->viewPath . '/../vendor/');
    Yii::app()->clientScript->registerScriptFile($vendorDir . '/farbtastic.js');
    Yii::app()->clientScript->registerCssFile($vendorDir . '/farbtastic.css');

    $this->pickerId = $this->htmlOptions['id'] . '-picker';
    Yii::app()->clientScript->registerScript($this->htmlOptions['id'] . '-loader', '$("#'.$this->pickerId.'").farbtastic("#'.$this->htmlOptions['id'].'")', CClientScript::POS_LOAD);
  }

  public function run()
  {
    $this->render('farbtastic');
  }
}
